public class Main {
    public static void change(ListNode A, ListNode B){
        B.next.next = A.next.next;
    }

    public static ListNode getIntersectionNode(ListNode headA, ListNode headB){
        if (headA == null || headB == null){
            return null;
        }
        ListNode cur1 = headA;
        ListNode cur2 = headB;
        int i = 0;
        int j = 0;
        while (cur1 != null){
            cur1 = cur1.next;
            i++;
        }
        while (cur2 != null){
            cur2 = cur2.next;
            j++;
        }
        cur1 = headA;
        cur2 = headB;
        if (i > j){
            int size = i - j;
            while (size != 0){
                cur1 = cur1.next;
                size--;
            }
        }
        else {
            int size = j - i;
            while (size != 0){
                cur2 = cur2.next;
                size--;
            }
        }
        while (cur1 != null && cur2 != null){
            if (cur1 == cur2){
                return cur1;
            }
            else {
                cur1 = cur1.next;
                cur2 = cur2.next;
            }
        }
        return null;
    }
    public static void main(String[] args) {
        MyLinkedList myLinkedList1 = new MyLinkedList();
        myLinkedList1.addLast(1);
        myLinkedList1.addLast(2);
        //myLinkedList1.addLast(3);
        //myLinkedList1.addLast(4);
        MyLinkedList myLinkedList2 = new MyLinkedList();
        myLinkedList2.addLast(5);
        myLinkedList2.addLast(7);
        myLinkedList2.addLast(8);
        myLinkedList2.addLast(9);
        //change(myLinkedList1.head,myLinkedList2.head);
        //System.out.println(getIntersectionNode(myLinkedList1.head, myLinkedList2.head));
//        MyLinkedList myLinkedList2 = new MyLinkedList();
//        myLinkedList2.addLast(1);
//        myLinkedList2.addLast(2);
//        myLinkedList2.addLast(3);
//        myLinkedList2.addLast(2);
//        myLinkedList2.addLast(5);
        //System.out.println(myLinkedList.contains(5));
        //myLinkedList.remove(4);
        //myLinkedList.addIndex(3, 6);
        //myLinkedList.removeAllKey(2);
        //myLinkedList.clear();
        //ListNode ret = myLinkedList.charge();
        //System.out.println(myLinkedList1.isPalindrome(myLinkedList1.head));
        //myLinkedList1.display();
//        myLinkedList2.display();
        //System.out.println(myLinkedList.size());
        //myLinkedList1.rotateRight(myLinkedList1.head, 2);
        //myLinkedList1.display();
        //myLinkedList2.funDisplay(myLinkedList2.head);
        myLinkedList2.funcDisplay(myLinkedList2.head);
    }
}